layui.use(['layer', 'jquery'], function () {
    let layer = layui.layer;
    let $ = layui.$;

    //提交表单数据
    $('#add').on('click', function () {
        if($("#addSubjectName").val()=='' ||　$("#addLevelName").val()==''){
            layer.msg('必填字段不能为空', { offset: 't', icon: 5 });
            return;
        }
        let form = $('#app').serialize();
        let subjectName = $('input[name="subjectName"]').val();
        let levelName = $('select[name="levelName"]').val();
        

        $.ajax({
            //根据学科名称查询，并把数据添加到对应的表单中
            url: 'http://localhost:8080/list/subject/name',
            type: 'get',
            data: { 'subjectName': subjectName, 'levelName': levelName },
            success: function (vo) {
                if (vo.code == 200) {
                    //请求添加
                    $.ajax({
                        url: 'http://localhost:8080/subject/add',
                        type: 'post',
                        data: form,
                        success: function (vo2) {
                            if (vo2.code == 200) {
                                layer.msg('添加成功', { offset: 't', icon: 1 });
                                // 延迟1秒执行代码
                                setTimeout(function () {
                                    // 重置表单
                                    $('#app')[0].reset();
                                    // location = 'subject_list.html';
                                }, 1000);
                                
                            } else if (vo2.code == 500) {
                                layer.msg(vo2.message, { offset: 't', icon: 2 });
                            }
                        }
                    });
                } else {
                    layer.msg(vo.message, { offset: 't', icon: 2 });
                }
            }
        });
    });
});